<template>
  <div class="like-received-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <h2>
            <i class="icon-heart">❤️</i>
            收到的点赞
          </h2>
          <p class="header-subtitle">
            查看用户对你发布的攻略的点赞
          </p>
        </div>
        <div class="total-count">
          <span class="count-number">{{ likes.length }}</span>
          <span class="count-text">条点赞</span>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <span class="loading-text">加载点赞数据中...</span>
    </div>

    <!-- 空状态 -->
    <div v-else-if="likes.length === 0" class="empty-state">
      <div class="empty-icon">
        <i class="icon-empty">💭</i>
      </div>
      <h3 class="empty-title">还没有收到点赞</h3>
      <p class="empty-description">分享更多精彩内容，吸引用户点赞吧！</p>
      <router-link to="/strategy/create" class="explore-btn">
        <i class="icon-explore">✏️</i>
        创建内容
      </router-link>
    </div>

    <!-- 点赞列表 -->
    <div v-else class="like-list">
      <div v-for="like in paginatedList" :key="like.strategyLikeId" class="like-card">
        <!-- 用户和攻略信息 -->
        <div class="like-info">
          <!-- 用户信息 -->
          <div class="user-section">
            <router-link :to="`/user/profile/${like.userId}`" class="user-link">
              <img :src="like.userAvatar || defaultAvatar" class="user-avatar" alt="用户头像" />
              <div class="user-details">
                <span class="user-name">{{ like.userName }}</span>
                <span class="like-action">点赞了你的攻略</span>
              </div>
            </router-link>
          </div>
          
          <!-- 攻略信息 -->
          <router-link :to="`/homepage/strategy/detail/${like.strategyId}`" class="strategy-section">
            <h3 class="strategy-title">{{ like.strategyName }}</h3>
            <div class="like-meta">
              <div class="meta-item">
                <i class="meta-icon">📅</i>
                <span>{{ like.likeDate }}</span>
              </div>
            </div>
          </router-link>
        </div>
        
        <!-- 攻略图片 -->
        <router-link :to="`/homepage/strategy/detail/${like.strategyId}`" class="like-image-container">
          <img :src="like.strategyImageLink" class="like-image" alt="攻略图片" />
          <div class="like-badge">
            <i class="badge-icon">❤️</i>
          </div>
        </router-link>
      </div>
    </div>

    <!-- 分页器 -->
      <div v-if="likes.length > pageSize" class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="likes.length"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import axios from 'axios';

export default {
  name: 'GetLikeInfo',
  data() {
    return {
      likes: [],
      loading: true,
      currentPage: 1,
      pageSize: 8,
      defaultAvatar: 'https://cdn.jsdelivr.net/gh/baimingxuan/media-store/avatar-default.png',
      defaultStrategyImage: 'https://img.tukuppt.com/ad_preview/00/08/96/5c0d5756314f4.jpg!/fw/780',
      baseStrategyImageUrl: 'http://localhost:8080/uploads/strategy/',
      baseAvatarUrl: 'http://localhost:8080/uploads/avatar/' // 添加头像基础路径
    };
  },
  computed: {
    paginatedList() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.likes.slice(start, start + this.pageSize);
    }
  },
  methods: {
    // 检测是否绝对路径
    isAbsoluteUrl(url) {
      return /^https?:\/\//i.test(url);
    },
    
    // 日期格式化方法
    formatDate(dateString) {
      if (!dateString) return '';
      try {
        const date = new Date(dateString);
        return date.toLocaleString('zh-CN', {
          year: 'numeric',
          month: '2-digit',
          day: '2-digit',
          hour: '2-digit',
          minute: '2-digit'
        }).replace(/\//g, '-');
      } catch (e) {
        console.error('日期格式化错误:', e);
        return dateString;
      }
    },
    
    // 获取点赞数据
    async fetchLikes() {
      this.loading = true;
      try {
        const loginAccount = localStorage.getItem('userAccount'); 
        if (!loginAccount) {
          this.likes = [];
          ElMessage.warning('未登录，请先登录');
          return;
        }
        
        const response = await axios.get(`http://localhost:8080/strlike/myLikes/${loginAccount}`);
        
        this.likes = response.data.map(like => ({
          ...like,
          // 处理头像路径
          userAvatar: like.userAvatar 
            ? this.isAbsoluteUrl(like.userAvatar) 
                ? like.userAvatar 
                : this.baseAvatarUrl + like.userAvatar
            : this.defaultAvatar,
          // 处理攻略图片路径
          strategyImageLink: like.strategyImageLink 
            ? this.isAbsoluteUrl(like.strategyImageLink) 
                ? like.strategyImageLink 
                : this.baseStrategyImageUrl + like.strategyImageLink 
            : this.defaultStrategyImage,
          // 格式化日期
          likeDate: this.formatDate(like.likeTime)
        }));
      } catch (error) {
        console.error('获取点赞失败:', error);
        ElMessage.error('获取点赞失败: ' + (error.response?.data?.message || error.message));
      } finally {
        this.loading = false;
      }
    },
    
    handlePageChange(page) {
      this.currentPage = page;
    }
  },
  mounted() {
    this.fetchLikes();
  }
};
</script>

<style scoped>
.like-received-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
  margin-bottom: 32px;
}

.header-content {
  background: white;
  padding: 24px 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left h2 {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-heart {
  font-size: 24px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.header-subtitle {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0;
}

.total-count {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(135deg, #a6a6e4 0%, #c3aadd 100%);
  color: white;
  padding: 16px 20px;
  border-radius: 12px;
  min-width: 80px;
}

.count-number {
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
}

.count-text {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 4px;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #f3f4f6;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 16px;
  color: #6b7280;
  font-weight: 500;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 16px;
  padding: 60px 40px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 24px;
  opacity: 0.6;
}

.empty-title {
  font-size: 20px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
}

.empty-description {
  font-size: 16px;
  color: #6b7280;
  margin: 0 0 32px 0;
  line-height: 1.5;
}

.explore-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.explore-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

/* 点赞列表 - 修改为长条形布局 */
.like-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

.like-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  min-height: 180px;
}

.like-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

/* 左侧信息区域 */
.like-info {
  padding: 24px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.user-section {
  margin-bottom: 16px;
}

.user-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: inherit;
}

.user-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #e2e8f0;
  transition: all 0.3s ease;
  margin-right: 12px;
}

.user-link:hover .user-avatar {
  border-color: #667eea;
  transform: scale(1.05);
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 4px;
}

.like-action {
  font-size: 14px;
  color: #6b7280;
}

.strategy-section {
  text-decoration: none;
  color: inherit;
}

.strategy-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.strategy-section:hover .strategy-title {
  color: #667eea;
}

.like-meta {
  display: flex;
  gap: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: #9ca3af;
}

.meta-icon {
  font-size: 14px;
}

/* 右侧图片区域 */
.like-image-container {
  position: relative;
  width: 240px;
  height: 180px;
  overflow: hidden;
  cursor: pointer;
}

.like-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.like-card:hover .like-image {
  transform: scale(1.05);
}

.like-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 6px 12px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #e74c3c;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.badge-icon {
  font-size: 12px;
  animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

/* 分页器 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .like-received-container {
    padding: 16px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .like-card {
    grid-template-columns: 1fr;
    min-height: auto;
  }
  
  .like-image-container {
    width: 100%;
    height: 200px;
    order: -1;
  }
  
  .like-info {
    padding: 20px;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 20px;
  }
  
  .header-left h2 {
    font-size: 24px;
  }
  
  .user-avatar {
    width: 40px;
    height: 40px;
  }
  
  .user-name {
    font-size: 15px;
  }
  
  .strategy-title {
    font-size: 16px;
  }
}
</style>